﻿using System;
using System.Web.UI.WebControls;
using Wicresoft.RDW.Presentation;
using Resources;
using Wicresoft.RDW.Utility;


namespace Wicresoft.RDW.Manage
{
    public partial class PageAuthorityManage : BasePage_BackEnd
    {
        private static int updateId = 0;
        private static bool status = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            pager.PageChange += new Wicresoft.RDW.UserControls.PagerControl.PageRefresh(PagerControll_PageChange);

            if (!IsPostBack)
            {
                status = false;
                ddlRoleId.DataSource = RolePresent.GetList();
                ddlRoleId.DataTextField = "Name";
                ddlRoleId.DataValueField = "RoleId";
                ddlRoleId.DataBind();
                ddlRoleId.Items.Insert(0, new ListItem(CommonResource.PleaseSelect, "-3"));
                ddlUserId.DataSource = UserPresent.GetList();
                ddlUserId.DataTextField = "Account";
                ddlUserId.DataValueField = "UserId";
                ddlUserId.DataBind();
                ddlUserId.Items.Insert(0, new ListItem(CommonResource.PleaseSelect, "-3"));
                pager.CurrentPageIndex = 0;
                PagerControll_PageChange(pager.CurrentPageIndex);
            }
        }

        protected void PagerControll_PageChange(int pageIndex)
        {
            string language = Language.ToString();
            var dataPagerSource = PriviledgePresent.GetPaged(pageIndex, pager.PageSize);
            gvPriviledge.DataSource = dataPagerSource;
            gvPriviledge.DataBind();
            var count = 0;
            count = dataPagerSource.TotalCount;
            pager.IntialProperties(count);
            lbtnStatus(status);
        }

        public override void InitControlResource()
        {
            int i = 0;
            gvPriviledge.Columns[i++].HeaderText = NameResource.Priviledge_RoleId;
            gvPriviledge.Columns[i++].HeaderText = NameResource.Priviledge_UserId;
            gvPriviledge.Columns[i++].HeaderText = NameResource.Priviledge_PagePath;
            gvPriviledge.Columns[i++].HeaderText = NameResource.Priviledge_Enable;
            gvPriviledge.Columns[i++].HeaderText = ManageResource.mgr_lblAction;
            lblRoleId.Text = NameResource.Priviledge_RoleId;
            lblUserId.Text = NameResource.Priviledge_UserId;
            lblPagePath.Text = NameResource.Priviledge_PagePath;
            lblEnable.Text = NameResource.Priviledge_Enable;
            btnSubmit.Text = NameResource.Btn_Submmit;
            btnCancel.Text = NameResource.btnCancel;
            btnAdd.Text = NameResource.Btn_Add;
            rfvtxtPagePath.ErrorMessage = NameResource.Priviledge_PagePath + CommonResource.NotNull;
        }
        protected void gvPriviledge_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Model.Priviledge priviledge = e.Row.DataItem as Model.Priviledge;
                Label lblEnable = e.Row.Cells[4].FindControl("lblEnable") as Label;
                Label lblRoleId = e.Row.Cells[4].FindControl("lblRoleId") as Label;
                Label lblUserId = e.Row.Cells[4].FindControl("lblUserId") as Label;
                if (priviledge.Enable == true)
                    lblEnable.Text = NameResource.Product_Status_Active;
                else if (priviledge.Enable == false)
                    lblEnable.Text = NameResource.Product_Status_Inactive;
                if (priviledge.RoleId != null && priviledge.RoleId > 0)
                {
                    var role = RolePresent.GetById((int)priviledge.RoleId);
                    if (role != null)
                    {
                        lblRoleId.Text = role.Name;
                    }
                }
                if (priviledge.UserId != null && priviledge.UserId > 0)
                {
                    var user = UserPresent.GetById((int)priviledge.UserId);
                    if (user != null)
                    {
                        lblUserId.Text = user.Account.ToString();
                    }
                }
                var lbtn = e.Row.Cells[3].FindControl("lbtnDelete") as LinkButton;
                lbtn.Text = ManageResource.mgr_btnDelete;
                lbtn.Attributes.Add("onclick", "return confirm('" + NameResource.Delete_Confirm + "');");
                (e.Row.Cells[3].FindControl("lbtnUpdate") as LinkButton).Text = ManageResource.mgr_btnEdit;
            }
        }

        protected void gvPriviledge_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int i = e.RowIndex;
            int priviledgeId = (int)this.gvPriviledge.DataKeys[i]["Id"];
            PriviledgePresent.Delete(priviledgeId);
            pager.CurrentPageIndex = 0;
            PagerControll_PageChange(pager.CurrentPageIndex);
        }

        protected void gvPriviledge_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            lblSubmitError.Text = string.Empty;
            int i = e.RowIndex;
            int priviledgeId = (int)this.gvPriviledge.DataKeys[i]["Id"];
            updateId = priviledgeId;
            var priviledge = PriviledgePresent.GetById(priviledgeId);
            if (priviledge != null)
            {
                if (priviledge.RoleId != null)
                {
                    ddlRoleId.SelectedValue = priviledge.RoleId.ToString();
                }
                else ddlRoleId.SelectedValue = "-3";
                if (priviledge.UserId != null)
                {
                    ddlUserId.SelectedValue = priviledge.UserId.ToString();
                }
                else ddlUserId.SelectedValue = "-3";
                txtPagePath.Text = priviledge.PagePath;
                cbxEnable.Checked = priviledge.Enable;
                PageStatus = Utility.PageStatus.Edit;
                phEdit.Visible = true;
                lbtnStatus(true);
                status = true;
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            lblSubmitError.Text = string.Empty;
            ddlRoleId.SelectedValue = "-3";
            ddlUserId.SelectedValue = "-3";
            txtPagePath.Text = string.Empty;
            cbxEnable.Checked = false;
            PageStatus = Utility.PageStatus.New;
            phEdit.Visible = true;
            lbtnStatus(true);
            status = true;
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            var roleid = int.Parse(ddlRoleId.SelectedValue);
            var userid = int.Parse(ddlUserId.SelectedValue);
            if (roleid == -3 && userid == -3)
            {
                lblSubmitError.Text = NameResource.Priviledge_RoleIdUserIdAllNull;
                return;
            }
            var pagepath = txtPagePath.Text;
            var enable = cbxEnable.Checked;
            switch (PageStatus)
            {
                case Utility.PageStatus.Edit:
                    var updatePriviledge = PriviledgePresent.GetById(updateId);
                    if (roleid == -3)
                        updatePriviledge.RoleId = null;
                    else
                        updatePriviledge.RoleId = roleid;
                    if (userid == -3)
                        updatePriviledge.UserId = null;
                    else
                        updatePriviledge.UserId = userid;            
                    updatePriviledge.PagePath = pagepath;
                    updatePriviledge.Enable = enable;
                    PriviledgePresent.Update(updatePriviledge);
                    break;
                case Utility.PageStatus.New:
                    if (roleid != -3 && userid != -3)
                    {
                        Model.Priviledge priviledge = new Model.Priviledge()
                        {
                            RoleId = roleid,
                            UserId = userid,
                            PagePath = pagepath,
                            Enable = enable
                        };
                        PriviledgePresent.Create(priviledge);
                    }
                    if (roleid != -3 && userid == -3)
                    {
                        Model.Priviledge priviledge = new Model.Priviledge()
                        {
                            RoleId = roleid,
                            PagePath = pagepath,
                            Enable = enable
                        };
                        PriviledgePresent.Create(priviledge);
                    }
                    if (roleid == -3 && userid != -3)
                    {
                        Model.Priviledge priviledge = new Model.Priviledge()
                        {
                            UserId = userid,
                            PagePath = pagepath,
                            Enable = enable
                        };
                        PriviledgePresent.Create(priviledge);
                    }
                    break;
            }
            phEdit.Visible = false;
            pager.CurrentPageIndex = 0;
            PagerControll_PageChange(pager.CurrentPageIndex);
            lbtnStatus(false);
            status = false;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            phEdit.Visible = false;
            pager.CurrentPageIndex = 0;
            PagerControll_PageChange(pager.CurrentPageIndex);
            lbtnStatus(false);
            status = false;
        }

        protected void lbtnStatus(bool status)
        {
            int count1 = gvPriviledge.Rows.Count;
            if (status)
            {
                for (int i = 0; i < count1; i++)
                {
                    LinkButton lbtnDelete = gvPriviledge.Rows[i].FindControl("lbtnDelete") as LinkButton;
                    lbtnDelete.Attributes.Add("disabled", "disabled");
                    lbtnDelete.Attributes.Remove("onclick");
                    lbtnDelete.Attributes.Add("onclick", "return false;");
                    LinkButton lbtnUpdate = gvPriviledge.Rows[i].FindControl("lbtnUpdate") as LinkButton;
                    lbtnUpdate.Attributes.Add("disabled", "disabled");
                    lbtnUpdate.Attributes.Remove("onclick");
                    lbtnUpdate.Attributes.Add("onclick", "return false;");
                }
            }
            else
            {
                for (int i = 0; i < count1; i++)
                {
                    LinkButton lbtnDelete = gvPriviledge.Rows[i].FindControl("lbtnDelete") as LinkButton;
                    lbtnDelete.Attributes.Remove("disabled");
                    lbtnDelete.Attributes.Remove("onclick");
                    lbtnDelete.Attributes.Add("onclick", "return confirm('" + NameResource.Delete_Confirm + "');");
                    LinkButton lbtnUpdate = gvPriviledge.Rows[i].FindControl("lbtnUpdate") as LinkButton;
                    lbtnUpdate.Attributes.Remove("disabled");
                    lbtnUpdate.Attributes.Remove("onclick");
                    lbtnUpdate.Attributes.Add("onclick", "return true;");
                }
            }
        }
    }
}